<template>
    <div>
        <dialog-page-list :title="title" confirmButton="确认" :show.sync="_show" :isMulti="false" :url="this.$constUrl.User_Choose_CompanyPageData" :params="params" :showClear="showClear" @callback="callback" @initList="_initList">
            <van-cell v-for="(item, index) in list" :key="index" @click="selectUser(item)">
                <div class="user-item">
                    <div>
                        <div class="mb-5 color-gray1">{{ item.CompanyName }}</div>

                    </div>
                    <div v-if="isMulti != 'f'">
                        <van-checkbox :value="isSelected(item)"></van-checkbox>
                    </div>
                </div>
            </van-cell>
        </dialog-page-list>
    </div>
</template>
<script>
import DialogPageList from "@/components/DialogPageList.vue";
export default {
    components: { DialogPageList },
    props: {
        title: {
            type: String,
            default: "选择下级单位",
        },
        show: {},
        showClear: {},
    },
    data() {
        return {
            list: [],
            isMulti: false,
            selected: [],
            selectedUsers: [],
            showSelectUser: false,
            params: [
                {
                    dataType: "orgCompanyList",
                    accounted: true,
                    userKind: 0,
                },
            ],
        };
    },
    computed: {
        _show: {
            get() {
                return this.show;
            },
            set(value) {
                this.$emit("update:show", value);
            },
        },
    },
    methods: {
        _initList(list) {
            this.list = list;
        },
        callback(flag) {
            let data = {};
            if (flag && this.selectedUsers.length > 0) {
                data.list = this.selectedUsers;
                data.CompanyCode = this.selectedUsers[0].CompanyCode;
                data.CompanyName = this.selectedUsers[0].CompanyName;
            } else {
                this.selected = [];
            }
            this.$emit("callback", data);
            this._show = false;
        },
        selectUser(item) {
            let companyCode = item.CompanyCode;
            if (this.isSelected(item)) {
                for (var i = 0; i < this.selected.length; i++) {
                    if (this.selected[i] == item.CompanyCode) {
                        this.selected.splice(i, 1);
                        this.selectedUsers.splice(i, 1);
                        break;
                    }
                }
            } else {
                if (this.isMulti == false) {
                    this.selected = [];
                    this.selectedUsers = [];
                }
                let flag = false;
                for (var i = 0; i < this.selected.length; i++) {
                    if (this.selected[i] == companyCode) {
                        flag = true;
                        this.selected.splice(i, 1);
                        this.selectedUsers.splice(i, 1);
                        break;
                    }
                }
                if (!flag) {
                    this.selected.push(companyCode);
                    this.selectedUsers.push(item);
                }
            }
            if (this.isMulti == false) {
                this.callback(true);
            }
        },
        isSelected(item) {
            let companyCode = item.CompanyCode;
            let flag = false;
            for (var i = 0; i < this.selected.length; i++) {
                if (this.selected[i] == companyCode) {
                    flag = true;
                    break;
                }
            }
            return flag;
        },
    },
};
</script>
<style scoped>
.user-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
</style>